package com.handu.luna.boss.controller;

import javax.validation.Valid;

import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.handu.luna.core.domain.user.UserLog;
import com.handu.luna.core.domain.user.UserLog.ConsumePointStatus;
import com.handu.luna.core.domain.user.UserLog.LevelPointStatus;

@Controller
@RequestMapping("/userlog/*")
public class UserLogController extends BaseController {
	
	@RequestMapping(value = {"index/{userId}","/",""})
    public BossModel index(@PathVariable long userId,@PageableDefault(page = 0,size = DEFAULT_PAGE_SIZE) Pageable pageable){
		
		return createModel("userlog/index")
				.add("userlogPage",context.getUserLogRepository().findByUserId(userId, pageable))
				.add("userId", userId);
		
	}
	
	@RequestMapping(value="add/{userId}")
	public BossModel add(@PathVariable long userId){
		return createModel("userlog/add")
				.add("userlog",new UserLog())
				.add("userId", userId)
				.add("LevelPointStatus", UserLog.LevelPointStatus.values())
				.add("ConsumePointStatus", UserLog.ConsumePointStatus.values())
				.add("user", context.getUserRepository().findOne(userId));
	}
	
	@RequestMapping(value="save/{userId}",method = RequestMethod.POST)
	public BossModel save(@Valid @ModelAttribute("userlog") final UserLog userlog, @PathVariable long userId, final BindingResult bindingResult, RedirectAttributes redirectAttributes,@PageableDefault(page = 0,size = DEFAULT_PAGE_SIZE) Pageable pageable){
		
		if(userlog.getLogLevelPoint() != 0 || userlog.getLogConsumePoint() !=0) {
			if(userlog.getLogLevelPoint() != 0){
				if(userlog.getLevelPointStatus().equals(LevelPointStatus.INCREASE)){
					context.getUserRepository().IncreaselLevelPointById(userId, userlog.getLogLevelPoint());
				}else{
					context.getUserRepository().ReduceLevelPointById(userId, userlog.getLogLevelPoint());
				}
			}
			if(userlog.getLogConsumePoint() != 0){
				if(userlog.getConsumePointStatus().equals(ConsumePointStatus.INCREASE)){
					context.getUserRepository().IncreaselConsumePointById(userId, userlog.getLogConsumePoint());
				}else{
					context.getUserRepository().ReduceConsumePointById(userId, userlog.getLogConsumePoint());
				}
			}
			String messagePrefix = userlog.getSucessFlashMessage();
			context.getUserLogRepository().save(userlog);
			return createModel("userlog/index")
					.add("userlogPage",context.getUserLogRepository().findByUserId(userId, pageable))
					.add("userId", userId)
					.add("message", messagePrefix);
		}else{
			context.getUserLogRepository().save(userlog);
			return createModel("userlog/index")
					.add("userlogPage",context.getUserLogRepository().findByUserId(userId, pageable))
					.add("userId", userId);
		}
		
		
		
		
	}
}
